<?php
namespace app\manager\controller;

class Caravan extends BaseController
{
    use \app\common\traits\controller\TraitUpload;
    /**
     * GET shwo caravan info list
     * @return [type] [description]
     */
    public function index()
    {
        $list = model('Caravans')->getCaravansLists(input('get.'));
        $this->assign('list',$list);
        return view('p/caravan/index');

    }
    /**
     * GET show caravan info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function read($id)
    {
        $list = model('Caravans')->getCaravanInfo($id);//halt($list->status);
          $this->assign('list',$list);//dump($list->getData());die;
          $this->assign('id',$id);
          //房车信息表
          $render= model('CarRenders')->getCarRendersLists();
          $this->assign('render',$render);
          //营地表
          $camps= model('Camps')->getCampList();
          $this->assign('camps',$camps);
          
          //品牌表
          $brands = model('CarBrands')->getCarBrandsLists();

          $this->assign('brand',$brands);
          //车类型/国别/燃油/准驾类型配置文件
          $carcates=model('CarCates')->getCarCateList();
          $carcates = array_column($carcates, 'name', 'id');
          $this->assign('carcates',$carcates);//halt($carcates);

          $country=model('CarCountries')->getCountriesList();
          $country = array_column($country, 'name', 'id');
          $this->assign('country',$country);

          $drive_type=model('DriverTypes')->getDriverTypesLists();
          $drive_type = array_column($drive_type, 'name', 'id');
          $this->assign('drive_type',$drive_type);

          //车系字段表
          $ziduans=model('Caravans')->getZiduna();
          $this->assign('ziduans',$ziduans);

          //获取当前房车所在某时间段的状态
          if ($list->status == '已激活') {
            $share = model('common/Caravans')->getCaravanStatus($id);
            $this->assign('share',$share['shares']);//获取日历表房车的分享状态
            $this->assign('trusts',$share['trusts']);//获取日历表房车的出租状态
            //$this->assign('disables',$share['disables']);//获取日历表房车的订单状态
            $this->assign('sharedisables',$share['disables']['shares']);//获取日历表房车分享订单状态
            $this->assign('trustdisables',$share['disables']['trusts']);//获取日历表房车托管订单状态
            $this->assign('notrustorderdisables',$share['disables']['trustsnoorder']);//获取日历表房车无订单托管订单状态
            $renta = model('Calendar')->where('caravan_id',$id)->where('trusts','1')->column('single_day_price', 'datetimes');
            $this->assign('renta',json_encode($renta));
          }
          
        return view('p/caravan/read');
    }

    public function create()
    {
      $list = model('CarTpls')->getCarTplLists();
      $this->assign('list',$list);
      return view('p/caravan/create');
    }

    public function create_m($moban_id)
    {
      $cartpl_info = model('CarTpls')->getCarTplInfo($moban_id);
      $this->assign('cartpl_info',$cartpl_info);//halt($cartpl_info);
      
      //营地表
      $camps= model('Camps')->getCampList();
      $this->assign('camps',$camps);
      //品牌表
      $brands = model('CarBrands')->getCarBrandsLists();
      $this->assign('brand',$brands);
      //车类型/国别/燃油/准驾类型配置文件
      $carcates=model('CarCates')->getCarCateList();
      $carcates = array_column($carcates, 'name', 'id');
      $this->assign('carcates',$carcates);
      //国别
      $country=model('CarCountries')->getCountriesList();
      $country = array_column($country, 'name', 'id');
      $this->assign('country',$country);
      //准驾类型
      $drive_type=model('DriverTypes')->getDriverTypesLists();
      $drive_type = array_column($drive_type, 'name', 'id');
      $this->assign('drive_type',$drive_type);
      //车系字段表
      $ziduans=model('Caravans')->getZiduna();
      $this->assign('ziduans',$ziduans);
      return view('p/caravan/create_m');
    }

    public function save()
    {
      $post_caravan_info['name'] = input('post.name');
      //房车编号
      $post_caravan_info['number'] = model("user/CaravanInfos")->quickRandom(12);

      $post_caravan_info['car_cates_id'] = input('post.carcate_id');
      $post_caravan_info['car_brands_id'] = input('post.brand_id');
      $post_caravan_info['grade'] = input('post.grade');
      //地区-国家、省份、城市
      $post_caravan_info['country'] = input('post.addressCountry');
      $post_caravan_info['province'] = input('post.addressProvince');
      $post_caravan_info['city'] = input('post.addressCity');

      $id = session('manager.id');
      $camp_id = model('CampManagers')->where('id',$id)->value('camp_id');

      if (!empty($camp_id)) {
        //营地id
        $post_caravan_info['services'] = $camp_id;
        $campinfo = model('Camps')->where('id',$camp_id)->find();
        $post_caravan_info['branch_id'] = $campinfo->branch_id;
        $post_caravan_info['area'] = $campinfo->area;
        $post_caravan_info['country'] = $campinfo->country;
        $post_caravan_info['province'] = $campinfo->province;
        $post_caravan_info['city'] = $campinfo->city;
      }
      $post_caravan_info['car_countries_id'] = input('post.carcountry_id');
      $post_caravan_info['driver_type'] = input('post.drivetype_id');
      $post_caravan_info['park_num'] = input('post.park');
      $post_caravan_info['single_bed_num'] = input('post.single_bed_num');
      $post_caravan_info['double_bed_num'] = input('post.double_bed_num');
      $post_caravan_info['sofe_num'] = input('post.sofe_num');
      $post_caravan_info['people_num'] = input('post.people_num');
      $post_caravan_info['size'] = input('post.size');
      $post_caravan_info['registration'] = input('post.register_date');
      if (input('post.plate_num') == '') {
        return $this->error('请添加车牌号');
      }
      $post_caravan_info['brand_number'] = input('post.plate_num');
      $post_caravan_info['speed'] = input('post.gear');
      $post_caravan_info['wifi'] = input('post.wifi');
      $post_caravan_info['kitchen'] = input('post.kitchen');
      $post_caravan_info['bathroom'] = input('post.bathroom');
      $post_caravan_info['tv'] = input('post.tv');
      $post_caravan_info['washer'] = input('post.washer');
      $post_caravan_info['air_condition'] = input('post.air_condition');
      $post_caravan_info['the_shower'] = input('post.the_shower');
      $post_caravan_info['extinguisher'] = input('post.extinguisher');
      $post_caravan_info['annunciator'] = input('post.annunciator');
      $post_caravan_info['monitored'] = input('post.monitored');
      
      //图片处理判断是否上传
      // if (!empty(input('post.caravanphoto/a'))) {
      //     $post_caravan_info['piccaravan'] = input('post.caravanphoto/a');
      // }
      // if (!empty(input('post.caravancover/a'))) {
      //     $post_caravan_info['caravancover'] = input('post.caravancover/a');
      // }

      if (empty(input('post.caravanphoto/a')) || empty(input('post.caravancover/a'))) {
        return $this->error('请添加房车封面图片或内容图片');
      }
      $post_caravan_info['piccaravan'] = input('post.caravanphoto/a');
      $post_caravan_info['caravancover'] = input('post.caravancover/a');
  
      $caravan = model('Caravans')->createCaravan($post_caravan_info);

      if($caravan){
          return $this->success('创建房车成功', '/caravan');
      }
          return $this->error('创建房车失败');
    }

    public function getRecycleInfo($id)
    {
        $data = model('Caravans')->getRecycleInfo(['id'=>$id], true);
        $this->assign('data',$data);
        return view('caravan/read');
    }

    /**
     * 关联设置
     */
    public function relation(){
      $caravan_id=input('get.id');
      $this->assign('caravan_id',$caravan_id);
      $camps=model('Camps')->getCamps();
      $this->assign('camps',$camps);
      return view('p/caravan/relation');
    }


	/**
	 * 房车价格设置表单
	 */
	 
	 public function price($id){
		$this->assign('id',$id);
		return view('p/caravan/price');
	 }
	
	/**
	 * 房车价格设置
	 */
	public function postPrice(){
		$id = input('post.id');
		$post_caravan_info['integral'] = input('post.integral');
		$caravan = model('Caravans')->updateCaravan($id, $post_caravan_info);

        if ($caravan) {

            return $this->success(lang('update caravanPrice success'), '/caravan');
        } 
        return $this->error(lang('update caravanPrice failed').$caravan['msg']);   
				
	}
	
	
    public function postRelation()
    {
      $caravan_id=input('post.caravan_id');//halt($caravan_id);

      $card_number=input('post.card_number');
      $phone=input('post.phone');
      $user_caravan_info=model('Cards')->where('phone',$phone)->find();
      if ($user_caravan_info) {
        return $this->error('此账户已关联营地泊位');
      }
      $camp_id=input('post.camp_id');//halt($camp_id);
      $parknumber=input('post.parknumber');//halt($parknumber);
      $park['camp_id']=$camp_id;
      $park['park_number']=$parknumber;
      $list=model('Parks')->where($park)->find();//halt($list);

      $list->caravan_id = $caravan_id;
      $list->status = self::PARK_RELATION_STATUS;
      
      $park_id=$list['id'];
      $count=model('Cards')->setCardToPark($park_id, $card_number, $phone ,$caravan_id);
      if ($count) {
        $list->save();
        return $this->success('营地泊位关联成功','/caravan/relation');
      }else{
            return $this->error('营地泊位关联失败');
        }
    }

    
    /**
     * GET edit caravan info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function edit($id)
    {
          $list = model('Caravans')->getCaravanInfo($id);//halt($list->conCaravan);
          $this->assign('list',$list);//dump($list->getData());die;
          $this->assign('id',$id);
          //房车信息表
          $render= model('CarRenders')->getCarRendersLists();
          $this->assign('render',$render);
          //营地表
          $camps= model('Camps')->getCampList();
          $this->assign('camps',$camps);
          
          //品牌表
          $brands = model('CarBrands')->getCarBrandsLists();

          $this->assign('brand',$brands);
          //车类型/国别/燃油/准驾类型配置文件
          $carcates=model('CarCates')->getCarCateList();
          $carcates = array_column($carcates, 'name', 'id');
          $this->assign('carcates',$carcates);//halt($carcates);

          $country=model('CarCountries')->getCountriesList();
          $country = array_column($country, 'name', 'id');
          $this->assign('country',$country);

          $drive_type=model('DriverTypes')->getDriverTypesLists();
          $drive_type = array_column($drive_type, 'name', 'id');
          $this->assign('drive_type',$drive_type);

          //车系字段表
          $ziduans=model('Caravans')->getZiduna();
          $this->assign('ziduans',$ziduans);

        return view('p/caravan/edit');
    }
    /**
     * PUT update caravan info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function update($id)
    {
        $post_caravan_info['name'] = input('post.name');
        $post_caravan_info['number'] = input('post.number');
        $post_caravan_info['car_brands_id'] = input('post.car_brands_id');
        $post_caravan_info['car_cates_id'] = input('post.car_cates_id');
        $post_caravan_info['speed'] = input('post.speed');
        $post_caravan_info['driver_type']=input('post.driver_type');//halt($post_caravan_info['driver_type']);
        $post_caravan_info['registration'] = input('post.registration');
        $post_caravan_info['car_countries_id']=input('post.car_countries_id');
        $post_caravan_info['services'] = input('post.services');
        $post_caravan_info['park_num'] = input('post.park_num');
        $post_caravan_info['size'] = input('post.size');
        $post_caravan_info['people_num'] = input('post.people_num');
        $post_caravan_info['single_bed_num'] = input('post.single_bed_num');
        $post_caravan_info['double_bed_num'] = input('post.double_bed_num');
        $post_caravan_info['sofe_num'] = input('post.sofe_num');
        $post_caravan_info['kitchen'] = input('post.kitchen');
        $post_caravan_info['bathroom'] = input('post.bathroom');
        $post_caravan_info['wifi'] = input('post.wifi');
        $post_caravan_info['tv'] = input('post.tv');
        $post_caravan_info['washer'] = input('post.washer');
        $post_caravan_info['air_condition'] = input('post.air_condition');
        $post_caravan_info['the_shower'] = input('post.the_shower');
        $post_caravan_info['extinguisher'] = input('post.extinguisher');
        $post_caravan_info['annunciator'] = input('post.annunciator');
        $post_caravan_info['monitored'] = input('post.monitored');
        // $post_caravan_info['picCaravan'] = input('post.picCaravan');
        // $post_caravan_info['annunciator'] = input('post.annunciator');
        
        $caravan = model('Caravans')->updateCaravan($id, $post_caravan_info);

        if ($caravan) {

            return $this->success(lang('update caravan success'), '/caravan');
        } 
        return $this->error(lang('update caravan failed').$caravan['msg']);        
    }
    /**
     * DELETE caravan info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete()
    {
        
        $ids=input('post.ids/a');
        if (empty($ids)) {
          return $this->error(lang('delete is null')); 
        }
        if (model('Caravans')->delCaravan($ids)) {
            return $this->success(lang('delete caravan success'), '/caravan');
        }    
        return $this->error(lang('delete caravan failed'));      
    }
    /**
     * 回收站数据
     * @return [type] [description]
     */
    public function getRecycle()
    {
        $page = input('get.p',1);
        $caravans = model('Caravans')->getCaravansListsOnlyTrashed();

        return view('p/caravan/recycle',['caravans'=>$caravans]);
    }
    /**
     * 恢复删除数据
     * @param  uuid $id 要恢复数据的id
     * @return [type]     [description]
     */
    public function reset()
    {
        $ids=input('post.ids/a');
        if (empty($ids)) {
          return $this->error(lang('reset is null')); 
        }
        if (model('Caravans')->caravanRestore($ids)) {
            return $this->success(lang('reset caravan success'), '/caravan');
        }    
        return $this->error(lang('reset caravan failed'));      
    }

    public function mainTain(){
        return view('p/caravan/maintain');
    }

    public function draft(){
        $query = $this->setQuerys();
        $list = model('Caravans')->getCaravansListsDrafts($query, input('get.order'));
        $this->assign('list',$list);
        return view('p/caravan/draft');
    }

    public function setStauts()
    {
        $ids=input('post.ids/a');
        if (empty($ids)) {
          return $this->error(lang('revise is null')); 
        }
        $status=input('post.status/d');
        if (model('Caravans')->setInfoStatus($ids, $status)) {
            return $this->success(lang('to  revise success'), '/caravan');
        }
        return $this->error(lang('to  revise failed'));
    }

    public function forcedel()
     {
       $ids=input('post.ids/a');
        if (empty($ids)) {
          return $this->error(lang('forcedel is null')); 
        }
        if (model('Caravans')->delCaravan($ids,true)) {
            return $this->success(lang('forcedel caravan success'), '/caravan');
        }    
        return $this->error(lang('forcedel caravan failed'));   
     }

    //地区插件
    /**
     * [getCountry 国家]
     * @return [type] [description]
     */
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();
        return json($country);
    }
    /**
     * [getProvince 省份]
     * @return [type] [description]
     */
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    /**
     * [getCity 城市]
     * @return [type] [description]
     */
    public function getCity()
    {
       
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
    /**
     * [getCamp 营地]申明'area_code'=>-99，为其他营地，即该城市没有营地，填写的营地。
     * 后台可以根据查找camp_id=-99,收集信息。
     * @return [type] [description]
     */
    public function getCamp()
    {
        $city = input('addressCity');
        //取审核通过的营地
        $camp = db('camps')->where('city',$city)->where('status',1)->field(['id as area_code','count as parent_code','name as area_name','type'])->select();
        $camp[] = ['area_code'=>-99,'parent_code'=>'','area_name'=>'其他','type'=>''];
        return json($camp);
    }

    public function postUploadImg()
    {

      if ($this->upload('file')) {
          $url = $this->getUploadInfos();
          return json(array(
                  "state" => 'SUCCESS',
                  "url" => $url['url'],
              ));
        }
    }

    public function card($id)
    {
      return view('p/caravan/index1');
    }

}

